#include "stdafx.h"
#include "ScriptEditorSolutionTree.h"
#include "ScriptEditorSolution.h"
#include "ScriptEditorWidget.h"
#include <LitEditorDialog.h>

namespace Lit
{
	namespace Editor
	{
		ScriptSolutionTree::ScriptSolutionTree( QWidget * parent ) 
			: QTreeWidget(parent)
		{
			setColumnCount( 1 );
			//connect( (QTreeWidget*)this, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(showScript(QTreeWidgetItem*, int)) );
			connect( (QTreeWidget*)this, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(_itemClicked(QTreeWidgetItem*, int)) );
			connect( (QTreeWidget*)this, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), this, SLOT(_itemDoubleClicked(QTreeWidgetItem*, int)) );
		}
		ScriptSolutionTree::~ScriptSolutionTree(){}
			
		void ScriptSolutionTree::setSolution( ScriptSolutionPtr sol ){ mSolution = sol; }
		ScriptSolutionPtr& ScriptSolutionTree::getSolution(){ return mSolution; }
		const ScriptSolutionPtr& ScriptSolutionTree::getSolution() const{ return mSolution; }

		QTreeWidgetItem*		ScriptSolutionTree::getRoot(){ return mRoot; }
		const QTreeWidgetItem*	ScriptSolutionTree::getRoot() const{ return mRoot; }

		void ScriptSolutionTree::newSolution( const std::string &name )
		{
			clear();
			mRoot = new QTreeWidgetItem( this, QStringList(name.c_str()) );
			mRoot->setExpanded( true );
			addTopLevelItem( mRoot );
		}

		QTreeWidgetItem * ScriptSolutionTree::addFile( const std::string &name )
		{
			QTreeWidgetItem * itm = new QTreeWidgetItem( mRoot, QStringList(name.c_str()) );
			mRoot->addChild( itm );
			itm->setExpanded( true );
			return itm;
		}
		void ScriptSolutionTree::_itemDoubleClicked(QTreeWidgetItem*itm, int col){
			ScriptSolutionItemPtr item(mSolution->findItem(itm));
			if( item ) emit scriptDoubleClicked( item );
		}
		void ScriptSolutionTree::_itemClicked(QTreeWidgetItem*itm, int col){
			ScriptSolutionItemPtr item(mSolution->findItem(itm));
			if( item ) emit scriptClicked( item );
		}
		void ScriptSolutionTree::showScript(QTreeWidgetItem*itm, int col){
			ScriptSolutionItemPtr item(mSolution->findItem(itm));
			if( item ) item->openEditor();
		}
		void ScriptSolutionTree::showContextMenu( const QPoint & pos )
		{
			QMenu *m(new QMenu(this));
			QAction * set_fl = m->addAction("Set FrameListener");
			QAction * ret = m->exec( pos );
			if( ret == set_fl )
			{
				ScriptSolutionItemPtr item(mSolution->getCurrentScript());
				if( item )
				{
					std::string n = item->getName();
					Dialog::inform( this, "Setting FrameListener", n );
				}
			}
			if( m ) delete m,m=NULL;
		}
		void ScriptSolutionTree::mousePressEvent( QMouseEvent * evt )
		{
			if( evt->buttons().testFlag( Qt::RightButton ) ||
				evt->buttons() & Qt::RightButton ||
				evt->button() == Qt::RightButton )
			{
				ScriptSolutionItemPtr item(mSolution->findItem( currentItem() ));
				if( item )
				{
					emit scriptRightClicked( item );
				}
				//showContextMenu( evt->pos() );
			}
		}
	};
}